<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\GoodsCategoryInterface;
use App\Repository\Contracts\GoodsCategoryJavaInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 19:36
 *
 * @Description: 商品分类
 */
class GoodsCategoryController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var GoodsCategoryInterface
     */
    protected $goodsCategory;

    /**
     * @var GoodsCategoryJavaInterface
     */
    protected $goodsCategoryJava;

    protected $uid;

    /**
     * GoodsCategoryController constructor.
     * @param Request $request
     * @param GoodsCategoryInterface $goodsCategory
     * @param GoodsCategoryJavaInterface $goodsCategoryJava
     */
    public function __construct(Request $request, GoodsCategoryInterface $goodsCategory, GoodsCategoryJavaInterface $goodsCategoryJava)
    {
        $this->request = $request;

        $this->goodsCategory = $goodsCategory;

        $this->goodsCategoryJava = $goodsCategoryJava;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $list = $this->goodsCategory->lists($limit, $page);

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->goodsCategory->detail($id);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data = $this->request->only(['name', 'status']);
            $id = $this->goodsCategory->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * Update
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updated($id)
    {
        try {
            $data = $this->request->only(['name', 'status']);
            $result = $this->goodsCategory->updated($id, $data);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function initCategory()
    {
        if (!env('DIRECT_SALE') || !env('JAVA_NETWORK')) {
            return $this->success();
        }

        try {
            $detail = $this->goodsCategory->findIdForFirst();
            $result = $this->goodsCategoryJava->syncList(isset($detail[0])?$detail[0]['created_at']:0);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}